(function () {/**
 * Created by ChenJiangwei on 2017/1/20.
 */
var baseInfoForm = function() {
    this.globalHost=common.globalHost;
    this.tel=$('#tel');
    this.password=$('#password');
    this.notice=$('#notice');
    this.form=$('form');
    this.submit=$('#login-btn');  
    this.searchVillageBtn=$('#search-btn');
};
baseInfoForm.prototype = {
    formPost:function (options) {
        var self=this;
        var url=options.url;
        var sendData=options.sendData;
        var resSuccess=options.resSuccess;
        var resFailed=options.resFailed;
        jQuery.support.cors = true;
        $.ajax
        ({
            data:sendData,
            type: 'POST',
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            dataType:"json",
            url:url,
            success:function(data){
                //console.log(data);
                if(typeof data==="string"){
                    data=JSON.parse(data);
                }
                if(!!data && data['code']=="0"){
                    sendData={};
                    self.changeMsg(data['res']['msg']);
                     !!resSuccess&&$.isFunction(resSuccess)&&resSuccess(data);
                }else{
                    self.changeMsg(data['res']['msg']);
                    !!resFailed&&$.isFunction(resFailed)&&resFailed(data);
                    if(data.code==101 ||data.code==102){
                        console.log("token为空或者token失效");
                        window.location.href='login.html';
                    }
                }
            },
            timeout:30000,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                self.searchVillageBtn.addClass('act-ago');
                self.changeMsg("网络不畅，请稍后重试");
            }
        });
    },
    buildSelectByDataList:function(options){
        var $selector=$(options.selector);
        var dataList=options.dataList;
        var initTitle=options.initTitle;
        var arrReturn=[];
        if(initTitle!=undefined){
            arrReturn.push("<option style='display:none;' value=''>"+initTitle+"</option>");
        }
        $.each(dataList,function (idx, ele) {
            arrReturn.push("<option value="+ele+">"+ele+"</option>");
        });
        $selector.html(arrReturn.join(""));
    },
     initForm:function(selector){
        var $selector=$(selector)[0];
        $selector.reset();
    },
    testText:function(options){
        var self=this;
        var noticeMsg='';
        var targetSelector=$(options.targetSelector);
        var msgStr=options.noticeMsg;
        var val=targetSelector.val();
        if(val===""){
            noticeMsg="请输入您的"+msgStr;
        }else if(val.indexOf(" ")>0){
            noticeMsg="您输入的"+msgStr+"有空格";
        }else if(val.length>30||val.length<5){
            noticeMsg="账号长度为5-30位";
        }else {
            return true;
        }
       self.changeMsg(noticeMsg);
        return false;
    },
    testPassword:function (options) {
        var noticeMsg='';
        var self=this;
        var targetSelector=$(options.targetSelector);
        var msgStr=options.noticeMsg;
        var sendValue=options.sendValue||"";
        var val=targetSelector.val()||sendValue;
        if(!val){
             noticeMsg="请输入您的"+msgStr;
        }else if(val.indexOf(" ")>0){
            noticeMsg="您输入的"+msgStr+"有空格";
        }else if(val.length>10||val.length<6){
            noticeMsg="密码为6-10位";
        }else{
            return true;
        }
         !!self.notice.length && self.changeMsg(noticeMsg);
            return false;
    },
    changeMsg:function(msg) {
        var self=this;
        !!self.notice.length && self.notice.html(msg).stop().animate({"opacity":"1"},500);
        setTimeout(function () {
            self.notice.animate({"opacity":"0"},500,function () {
                self.notice.html("");
            });
        },1000);
    }
};

window.baseInfoForm = new baseInfoForm();
}).call(this);